package com.example.demo.day0301;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class BufferedCopyFile {
    public static void main(String[] args) {
        try {
            BufferedCopyFile.copyFile1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void copyFile1() throws Exception {
        BufferedInputStream br = new BufferedInputStream(new FileInputStream("D:\\demo\\src\\main\\java\\com\\example\\demo\\day0301\\tt1.txt"));
        //缓冲字节输入流

        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("D:\\demo\\src\\main\\java\\com\\example\\demo\\day0301\\tt2.txt"));
        //缓冲字节输出流

        byte[] b = new byte[1024];

        int len = 0;//设置一个每次读取到的数据的长度

        while((len = br.read(b)) != -1){//直到br.read执行到没有字符串，此时返回值为-1。不再读取文件
            bo.write(b,0,len);//写到内存中
        }
        bo.flush();//刷到硬盘上
        bo.close();
        br.close();//先开后关
    }
}
